package com.example.tijianserver.response;

//统一响应格式
public class ApiRestResponse<T> {
    private Integer status;  //约定: 成功总是200,失败才各种各样的值
    private String desc;
    //定义泛型,需要在类上声明泛型
    private T data;

    private final static Integer SUCCESS_OK=200;
    private final static String SUCCESS_DESC="处理成功";
    private final static Integer FAIL_OK=500;
    private final static String FAIL_DESC="处理失败";


    //定制一个构造方法，含状态码和描述信息的初始化
    public ApiRestResponse(Integer status,String desc)
    {
        this.status=status;
        this.desc=desc;
    }

    //定制一个构造方法，含状态码和描述信息，数据data，初始化
    public ApiRestResponse(Integer status,String desc,T data)
    {
        this.status=status;
        this.desc=desc;
        this.data=data;
    }

    //构造方法: 构造成功 构造失败

    //获取对应成功的api响应实例
    public static <T> ApiRestResponse<T> success(){
        return new ApiRestResponse<>(SUCCESS_OK,SUCCESS_DESC);
    }
    public static <T> ApiRestResponse<T> success(T data){
        return new ApiRestResponse<>(SUCCESS_OK,SUCCESS_DESC,data);
    }

    //获取对应失败的api响应实例
    public static <T> ApiRestResponse<T> error(TijianExceptionEnum e){
        return new ApiRestResponse<>(e.getStatus(),e.getDesc());
    }
    public static <T> ApiRestResponse<T> error()
    {
        //ApiRestResponse<T> response=new ApiRestResponse<>(status, desc,data);

        return new ApiRestResponse<>(FAIL_OK, FAIL_DESC);
    }

    public static <T> ApiRestResponse<T> error(Integer status,String desc)
    {
        return new ApiRestResponse<>(status, desc);
    }


    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
